﻿using Devil;
using UnityEditor;
using UnityEngine;

namespace GameToolkit.Editor
{
    [CustomEditor(typeof(ActorBehaviour)), CanEditMultipleObjects]
    public class ActorBehaviourEditor : SharingConfigMonoEditor
    {
        [SerializeField]
        bool m_DropRuntime;

        string GetDescription(object obj)
        {
            if (obj == null)
                return "-";
            if (obj is IDebugContext ctx)
            {
                var context = ctx.GetContext();
                if (context != null)
                    return $"{context.name} ({context.GetType().Name})";
            }
            return $"<{obj.GetType().Name}>";
        }

        protected override void OnEnable()
        {
            base.OnEnable();
            m_DropRuntime = EditorPrefs.GetBool("actor.rtst", false);
        }

        protected override void OnDisable()
        {
            base.OnDisable();
            EditorPrefs.SetBool("actor.rtst", m_DropRuntime);
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            var repaint = false;
            if(!serializedObject.isEditingMultipleObjects)
            {
                EditorGUILayout.BeginVertical("helpbox");
                m_DropRuntime = EditorGUILayout.Foldout(m_DropRuntime, "Runtime States.", true);
                if (m_DropRuntime)
                {
                    EditorGUI.indentLevel++;
                    var actor = target as ActorBehaviour;
                    var pose = actor.Poseture;
                    EditorGUILayout.LabelField("Poseture", pose == null ? "-" : $"{pose.PosetureState}: {GetDescription(pose)}");
                    EditorGUILayout.LabelField("Base Ability", GetDescription(actor.BaseAbility));
                    EditorGUILayout.LabelField("Additional Ability", GetDescription(actor.AdditionalAbility));
                    EditorGUILayout.LabelField("Controller", GetDescription(actor.CurrentController));
                    EditorGUI.indentLevel--;
                    repaint = Application.isPlaying;
                }
                EditorGUILayout.EndVertical();
            }
            if (repaint)
                Repaint();
        }
    }
}
